package com.gitee.cirnochat.chatai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.cirnochat.chatai.mapper.AiChatSessionMapper;
import com.gitee.cirnochat.chatai.model.entity.AiChatSession;
import com.gitee.cirnochat.chatai.model.vo.req.aisession.AISessionUpdateDTO;
import com.gitee.cirnochat.chatai.model.vo.resp.AIChatSessionVo;
import com.gitee.cirnochat.chatai.service.AiChatSessionService;
import com.gitee.cirnochat.common.exception.BusinessException;
import com.gitee.cirnochat.common.model.enums.ErrorCode;
import com.gitee.cirnochat.common.model.vo.resp.PageVo;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author FlanChan
* @description 针对表【ai_chat_session】的数据库操作Service实现
* @createDate 2025-05-14 20:09:13
*/
@Service
@RequiredArgsConstructor
public class AiChatSessionServiceImpl extends ServiceImpl<AiChatSessionMapper, AiChatSession>
    implements AiChatSessionService {

    @Override
    public PageVo<AIChatSessionVo> pageSessionList(String searchName, int page, int size, Long userId) {
        LambdaQueryWrapper<AiChatSession> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiChatSession::getUserId, userId);
        if (searchName != null && !searchName.isEmpty()) {
            queryWrapper.like(AiChatSession::getSessionName, searchName);
        }
        queryWrapper.orderByDesc(AiChatSession::getUpdateTime);
        IPage<AiChatSession> pageInfo = new Page<>(page, size);
        this.page(pageInfo, queryWrapper).getRecords();
        IPage<AIChatSessionVo> pageInfoVo = pageInfo.convert(AIChatSessionVo::new);
        return new PageVo<>(pageInfoVo);
    }

    @Override
    public Boolean removeSessionById(Long id, Long userId) {
        AiChatSession existsSession = this.getById(id);
        if (existsSession == null) {
            return false;
        }
        if (!existsSession.getUserId().equals(userId)) {
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR, "没有权限删除该会话");
        }
        return this.remove(new LambdaQueryWrapper<>(AiChatSession.class)
                .eq(AiChatSession::getId, id)
                .eq(AiChatSession::getUserId, userId)
        );
    }

    @Override
    public AiChatSession updateSessionById(AISessionUpdateDTO sessionUpdateDTO, Long userId) {
        AiChatSession aiChatSession = this.getById(sessionUpdateDTO.getId());
        if (aiChatSession == null) {
            return null;
        }
        if (!aiChatSession.getUserId().equals(userId)) {
            return null;
        }
        aiChatSession.setSessionName(sessionUpdateDTO.getSessionName());
        aiChatSession.setUpdateTime(LocalDateTime.now());

        boolean updated = this.updateById(aiChatSession);
        if (!updated) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新会话失败");
        }

        return aiChatSession;
    }

    @Override
    @Async
    public void updateSessionInfoByIdAsync(AiChatSession aiChatSession) {
        this.updateById(aiChatSession);
    }
}




